home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Ddraw.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
6KB
|
220 lines
#include "stdafx.h"
LPDIRECTDRAW4 DD = 0;
DDPIXELFORMAT pixelformat;
DDCAPS hardware_caps;
LPDIRECTDRAWSURFACE4 screen = 0, backbuffer = 0;
LPDIRECTDRAWPALETTE palette = 0;
LPDIRECTDRAWCLIPPER clipper = 0;
LPPALETTEENTRY rgb_table = 0;
int color_table_this_mode[256];
int black, gray, white, red, green, blue, yellow, mask_color;
int inawin = TRUE, no_blit_hardware = FALSE, hardware_blit_caps = FALSE, no_parallax = FALSE;
double gamma;
static void init_directdraw()
{
LPDIRECTDRAW DD1;
// Get the DirectDraw4 object
if (FAILED(DirectDrawCreate(0, &DD1, 0)))
error("Unable to create DirectDraw interface\n\n"
"Did you install DirectX 6.0 or higher?\n\n"
"Do you have a video card/driver that can handle 640x480 at 256 colors?");
if (FAILED(DD1->QueryInterface(IID_IDirectDraw4, (LPVOID *)&DD)))
error("Unable to get the DirectDraw4 interface\n\n"
"Did you install DirectX 6.0 or higher?\n\n"
"Do you have a video card/driver that can handle 640x480 at 256 colors?");
DD1->Release();
// Get hardware capabilities
hardware_caps.dwSize = sizeof(hardware_caps);
if (FAILED(DD->GetCaps(&hardware_caps, 0)))
error("Unable to get hardware capabilities");
// Do we want to use hardware blitting?
hardware_blit_caps = hardware_caps.dwCaps & DDCAPS_BLT &&
(hardware_caps.dwCaps & DDCAPS_COLORKEY) &&
(hardware_caps.dwCKeyCaps & DDCKEYCAPS_SRCBLT);
}
void gamma_correct(BYTE &value)
{
value = (BYTE)(pow(255, 1 - gamma) * pow(value, gamma) + 0.5);
}
static void init_palette()
{
// Get pixelformat of current mode
pixelformat.dwSize = sizeof(pixelformat);
if (FAILED(screen->GetPixelFormat(&pixelformat)))
error("Unable to get pixelformat of current video mode");
// Load game palette
rgb_table = (PALETTEENTRY *)read_part_file("BLASTER.PAL", 24, 1024);
// Do gamma correction
for (int i = 0; i < 256; i++)
{
gamma_correct(rgb_table[i].peRed);
gamma_correct(rgb_table[i].peGreen);
gamma_correct(rgb_table[i].peBlue);
}
// Do some palette things when we're in palette mode
if (pixelformat.dwFlags & DDPF_PALETTEINDEXED8 && !inawin)
{
// Create the palette
if (FAILED(DD->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256, rgb_table, &palette, 0)))
error("Unable to make a 256 color palette");
// Set game palette
screen->SetPalette(palette);
}
// Get standard colors
mask_color = match_color(PINK);
black = match_color(BLACK);
gray = match_color(GRAY);
white = match_color(WHITE);
red = match_color(RED);
green = match_color(GREEN);
blue = match_color(BLUE);
yellow = match_color(YELLOW);
// Get translation table for this mode
for (int c = 0; c < 256; c++)
color_table_this_mode[c] = match_color(RGB(getr(c), getg(c), getb(c)));
}
void init_directdraw_inawin()
{
init_directdraw();
// Be nice
if (FAILED(DD->SetCooperativeLevel(0, DDSCL_NORMAL)))
error("Unable to set cooperative level for screen");
// Setup primary surface
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if (FAILED(DD->CreateSurface(&ddsd, &screen, 0)))
error("Unable to create primary surface");
// Make a backbuffer
if ((backbuffer = create_surface(SCREEN_X, SCREEN_Y, no_blit_hardware || !hardware_blit_caps? DDSCAPS_SYSTEMMEMORY : 0, 0)) == 0)
error("Unable to create backbuffer");
init_palette();
}
void init_directdraw_clipper()
{
// Attach a clipper to our window
if (FAILED(DD->CreateClipper(0, &clipper, 0)))
error("Unable to make clipper");
if (FAILED(clipper->SetHWnd(0, gamewindowhandle)))
error("Unable to associate clipper with main window");
if (FAILED(screen->SetClipper(clipper)))
error("Unable to associate clipper with directdraw primary surface");
}
void init_directdraw_notinawin()
{
init_directdraw();
// Set display mode and cooperative level
if (FAILED(DD->SetCooperativeLevel(mainwindowhandle, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
error("Unable to acquire the whole screen.");
if (FAILED(DD->SetDisplayMode(SCREEN_X, SCREEN_Y, 8, 0, 0)))
error("Unable to set video mode to 640x480x8\n"
"You may need to install the correct driver for your display card/monitor.");
// Setup primary surface
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
if (FAILED(DD->CreateSurface(&ddsd, &screen, 0)))
error("Unable to create primary surface!");
clear(screen, 0);
// Get the backbuffer
DDSCAPS2 ddscaps;
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
if (FAILED(screen->GetAttachedSurface(&ddscaps, &backbuffer)))
error("Unable to get backbuffer for primary surface");
init_palette();
}
void deinit_directdraw()
{
safe_release(&clipper);
safe_release(&backbuffer);
safe_release(&screen);
safe_release(&palette);
safe_delete(&rgb_table);
safe_release(&DD);
}
int draw_ok(HRESULT result)
{
switch(result)
{
case DD_OK:
return TRUE;
case DDERR_SURFACELOST:
if (FAILED(DD->RestoreAllSurfaces()))
error("Unable to restore surfaces!");
return FALSE;
case DDERR_WRONGMODE:
error("Video mode changed!");
}
ASSERT(result != DDERR_INVALIDRECT);
ASSERT(result != DDERR_INVALIDPARAMS);
ASSERT(FALSE);
return TRUE;
}